<!--#include file="./header.inc"-->
<%

select case aq
	'book
	case "save" 		: 	call save
	case "edit" 		: 	call edit
	case "delete" 		: 	call delete
	case "status" 		: 	call status
	case "editbatch" 	: 	call editBatch
	case "savebatch" 	: 	call saveBatch
	case "detail" 		: 	call detail
	case else 			: 	call index
end select

call db.close()

function index()
	dim keyword
	classID = admin.joinID(request("classID"))
	keyword = admin.filter(request("keyword"))
	sql = "select id,userName,dateTime,userIP,userAddr,userEmail,editCode,auditing,isTop,isShow,title,content,classID,hits"
	sql = sql & " from cj_book where replyID=0"
	if keyword <> "" then sql = sql & " and(title like '%" & keyword & "%' or content like '%" & keyword & "%')"
	if classID <> 0 then sql = sql & " and classID in(" & classID & ")"
	sql = sql & " Order by isTop,id desc"
	temp = db.arrayData(sql, page, rows)
	infoTotal = temp(0)
	data = temp(1)
	%><!--#include file="./view/book.index.html"--><%
end function

function detail()
	dim subject, reply, keyword
	keyword = admin.filter(request("keyword"))
	if iid = 0 then admin.message "id不存在！"
	sql = "select id,userName,dateTime,userIP,userAddr,userEmail,editCode,auditing,isTop,isShow,title,content,classID,hits"
	sql = sql & " from cj_book where replyID=0 and id=" & iid
	subject = db.getData(sql)
	if Not(isArray(subject)) then admin.message "id不存在！"
    sql = "select id,userName,dateTime,userIP,userAddr,userEmail,editCode,auditing,isTop,isShow,title,content"
    sql = sql & " from cj_book where replyID=" & iid
    if keyword <> "" then sql = sql & " and content like '%" & keyword & "%'"
    sql = sql & " Order By isTop,id asc"
    temp = db.arrayData(sql, page, rows)
    infoTotal = temp(0)
    reply = temp(1)
	%><!--#include file="./view/book.detail.html"--><%
end function

function edit()
	data = array("",adminInfo(2),userIP,"","","","","",now,0,1,1,0,"",0,"")
	if iid > 0 then
	    sql = "select title,userName,userIP,userAddr,editCode,userEmail,reAdmin,hits,dateTime,isTop,auditing,isShow,classID,content,replyID from cj_book where id=" & iid
		set rs = db.exec(sql, 1)
		if rs.eof then admin.message "id不存在！"
		data = rs
        set rs = nothing
	end if
	%><!--#include file="./view/book.edit.html"--><%
end function

function save()
	dim userName, userIP, userAddr, replyID, reAdmin
	replyID = app.int(request.form("replyID"))
	title = admin.filter(request.form("title"))
	userName = admin.filter(request.form("userName"))
	userIP = admin.filter(request.form("userIP"))
	reAdmin = app.int(request.form("reAdmin"))
	userAddr = trim(request.form("userAddr"))
	content = trim(request.form("content"))
	
	if len(content) < 10 then admin.message "内容字数太少！"
	content = admin.enUploadDir(content)
	if userName = "" then userName = siteName
	if userIP = "" then userIP = "保密"
	if userAddr = "" then userAddr = "保密"
	if iid > 0 then
		set rs = db.exec("select * from cj_book where id=" & iid, 3)
		if rs.eof then admin.message "id不存在！"
	else
		replyID = 0
		set rs = db.exec("select * from cj_book", 3)
		rs.AddNew
	end if
	app.setCookie "reAdmin", reAdmin
	rs("title") = title
	rs("reAdmin") = reAdmin
	rs("userName") = userName
	rs("userIP") = userIP
	rs("userAddr") = userAddr
	rs("content") = content
	rs("classID") = app.int(request.form("classID"))
	rs("replyID") = replyID
	rs("dateTime") = app.cj_date(request.form("dateTime"))
	rs("hits") = app.int(request.form("hits"))
	rs("isTop") = admin.isCheck(request.form("isTop"))
	rs("auditing") = admin.isCheck(request.form("auditing"))
	rs("isShow") = admin.isCheck(request.form("isShow"))
	rs("userEmail") = admin.filter(request.form("userEmail"))
	rs("editCode") = app.int(request.form("editCode"))
	rs.update
	rs.close : set rs = nothing
	if iid > 0 then
		admin.back "修改成功！", "返回列表", "?q=index"
	else
		admin.back "添加成功！", "继续添加", "?q=edit"
	end if
end function

function editBatch()
	%><!--#include file="./view/book.editBatch.html"--><%
end function

function saveBatch()
	dim cid, isClassID, isHits, isEditCode, isDateTime

	isHits = admin.isCheck(request.form("isHits"))
	isClassID = admin.isCheck(request.form("isClassID"))
	isEditCode = admin.isCheck(request.form("isEditCode"))
	isDateTime = admin.isCheck(request.form("isDateTime"))
	if not(isHits) and not(isClassID) and not(isEditCode) then admin.message "需要选择一项！"
	if isClassID then
		cid = app.int(request.form("classID"))
		if isArray(app.getClassInfo(cid)) then
			if app.getClassInfo(cid)(1) <> 2 then admin.message "不属于留言栏目分类！"
		else
			admin.message "栏目分类不存在！"
		end if
	end if
	dim hits, editCode
	editCode = app.int(request.form("editCode"))
	hits = app.int(request.form("hits"))
	set rs = db.exec("select * from cj_book where id in(" & batchId & ")", 3)
	do until rs.eof
		if isEditCode then rs("editCode") = editCode
		if isClassID then rs("classID") = cid
		if isHits then rs("hits") = hits
		rs.update
		rs.movenext
	loop
	rs.close : set rs = nothing
	admin.back "修改成功！", "返回列表", "?q=index"
end function

function delete()
    call db.exec("Delete from cj_book where replyID in(" & batchId & ")", 1)
	call db.exec("Delete from cj_book where id in(" & batchId & ")", 1)
	app.json "删除成功！", 0
end function

function status()
	dim auditing, isTop, msg
	auditing = request("auditing")
	if auditing <> "" then
        auditing = app.cj_bool(auditing)
        sql = " auditing=" & auditing
        msg = "取消审核"
        if auditing then
            msg = "审核"
        end if
    else
        isTop = app.cj_bool(request("isTop"))
        sql = " isTop=" & isTop
        msg = "取消置顶"
        if auditing then
            msg = "置顶"
        end if
    end if
	call db.exec("update cj_book set " & sql & " where id in(" & batchId & ")", 1)
	app.json msg & "成功！", 0
end function

%>